public class Solution {
    public static List<List<Integer>> permute(int[] num) {
        List<List<Integer>> res = new ArrayList<>();
        perm(num, 0, res);
        return res;
    }

    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static void perm(int[] nums, int start, List<List<Integer>> res) {
        if(start >= nums.length) {
            List<Integer> tmp = new ArrayList<>(nums.length);
            for(int n : nums){
                tmp.add(n);
            }
            res.add(tmp);
            return;
        }
        for(int i = start; i < nums.length; i++) {
            swap(nums, start, i);
            perm(nums, start + 1, res);
            swap(nums, start, i);
        }
    }
}